package org.team4.service;

import java.util.Date;
import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.team4.constants.SQLConstant;
import org.team4.constants.StringConstant;
import org.team4.dao.DAOFactory;
import org.team4.dao.meta.RoleDAO;
import org.team4.dao.meta.RoleModuleDepartmentsDAO;
import org.team4.dao.meta.RoleModuleFieldDAO;
import org.team4.dao.meta.RoleModuleOperateDAO;
import org.team4.dao.meta.RoleModuleUsersDAO;
import org.team4.dao.meta.RoleUsersDAO;
import org.team4.dao.meta.ScopeDataVisibleDAO;
import org.team4.domain.Role;
import org.team4.exception.CRUDException;
import org.team4.util.LoginBean;
import org.team4.util.Meta;
import org.team4.util.Validate;
import org.team4.vo.Page;

import org.team4.service.IService;

@Service
public class RoleService implements IService {

    private RoleDAO roleDAO;
    private RoleModuleOperateDAO roleOperateDAO;
    private RoleModuleFieldDAO roleModulFieldDAO;
    private RoleModuleUsersDAO roleModuleUsersDAO;
    private RoleModuleDepartmentsDAO roleModuleDepartmentsDAO;
    private RoleUsersDAO roleUsersDAO;
    private ScopeDataVisibleDAO scopeDataVisibleDAO;

    @Autowired
    public void setSessionFactory(SessionFactory sessionFactory) {
        roleDAO = (RoleDAO) DAOFactory.getDAO(sessionFactory, RoleDAO.class);
        roleOperateDAO = (RoleModuleOperateDAO) DAOFactory.getDAO(sessionFactory, RoleModuleOperateDAO.class);
        roleModulFieldDAO = (RoleModuleFieldDAO) DAOFactory.getDAO(sessionFactory, RoleModuleFieldDAO.class);
        roleModuleUsersDAO = (RoleModuleUsersDAO) DAOFactory.getDAO(sessionFactory, RoleModuleUsersDAO.class);
        roleModuleDepartmentsDAO = (RoleModuleDepartmentsDAO) DAOFactory.getDAO(sessionFactory, RoleModuleDepartmentsDAO.class);
        roleUsersDAO = (RoleUsersDAO) DAOFactory.getDAO(sessionFactory, RoleUsersDAO.class);
        scopeDataVisibleDAO = (ScopeDataVisibleDAO) DAOFactory.getDAO(sessionFactory, ScopeDataVisibleDAO.class);
    }

    /**
     * 分页角色列表
     * 
     * @param page
     * @return
     * @throws CRUDException
     */
    public Page<Role> listPagination(Page<Role> page) throws CRUDException {
        return roleDAO.pagination(page, SQLConstant.ROLE_PAGE_HQL + SQLConstant.getUsersWhere(Meta.getUsers(StringConstant.ROLE_ID)), null);
    }

    /**
     * 保存角色
     * 
     * @param role
     * @throws CRUDException
     */
    public void saveRole(Role role) throws CRUDException {
        role.setCreateTime(new Date());
        role.setCreator(LoginBean.getLoginBean().getUser());
        roleDAO.save(role);
    }

    /**
     * 查找所有角色
     * 
     * @return
     * @throws CRUDException
     */
    public List<Role> findAllRole() throws CRUDException {
        return roleDAO.findAll();
    }

    /**
     * 查找权限范围内的角色记录
     * 
     * @return
     * @throws CRUDException
     */
    public List<Role> findUsersWhereRole() throws CRUDException {
        return roleDAO.find(SQLConstant.ROLE_PAGE_HQL + SQLConstant.getUsersWhere(Meta.getUsers(StringConstant.ROLE_ID)));
    }

    /**
     * 获得角色
     * 
     * @param id
     * @return
     * @throws CRUDException
     */
    public Role findRoleById(String id) throws CRUDException {
        return roleDAO.get(id);
    }

    /**
     * 修改角色
     * 
     * @param role
     * @throws CRUDException
     */
    public void update(Role role) throws CRUDException {
        Role oldRole = roleDAO.get(role.getId());
        oldRole.setName(role.getName());
        oldRole.setRemark(role.getRemark());
        oldRole.setEditor(LoginBean.getLoginBean().getUser());
        oldRole.setEditTime(new Date());
        roleDAO.update(oldRole);
    }

    /**
     * 删除角色
     * 
     * @param idList
     * @throws CRUDException
     */
    @SuppressWarnings("unchecked")
    public void delete(List<String> idList) throws CRUDException {
        if (Validate.collectionNotNull(idList)) {
            for (String id : idList) {
                Role r = roleDAO.get(id);
                if(r==null)
                	return;
                // 删除相关记录
                roleOperateDAO.deleteAll(r.getRoleModuleOperates());
                roleModulFieldDAO.deleteAll(r.getRoleModuleFields());
                roleModuleUsersDAO.deleteAll(r.getRoleModuleUsers());
                roleUsersDAO.deleteAll(r.getRoleUsers());
                scopeDataVisibleDAO.deleteAll(r.getScopeDataVisibles());
                roleModuleDepartmentsDAO.deleteAll(r.getRoleModuleDepartments());
                roleDAO.delete(r);
            }
        }
    }
    
    /**
     * 判断同类下是否有相同的名称
     * 
     * @param name
     * @param superId
     * @return
     * @throws CRUDException
     */
    public boolean nameExist(String roletId, String name) throws CRUDException {
    	if(Validate.notNull(roletId)) {
    		return Validate.collectionNotNull(roleDAO.find(SQLConstant.ROLE_UPDATE_VALIDATE_NAME, roletId, name));
    	} else {
    		return Validate.collectionNotNull(roleDAO.find(SQLConstant.ROLE_VALIDATE_NAME, name));
    	}
    }
}